package cn.wyl.org.controller;

import cn.wyl.basic.annotation.PreAuthorize;
import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.PageList;
import cn.wyl.org.domain.Department;
import cn.wyl.org.query.DepartmentQuery;
import cn.wyl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@Controller
//@ResponseBody
@RequestMapping("/department")
@Api(value = "部门相关功能的API",description = "部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //写一个接口
    //1.请求方式 2.请求参数 3.请求地址 4.返回数据格式
    //Get http://localhost/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @PreAuthorize(name = "查询单个部门",sn = "department:getOne")
    //@PathVariable("id" ) Long id -从路径上去获取变量id 赋值给Long id
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    //Get http://localhost/department
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询所有部门",sn = "department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    //post http://localhost/department 对象
    @PostMapping
    @ApiOperation(value = "分页查询")
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改接口
     * @param department
     * @return
     */
    //put http://localhost/department {对象}
    @PutMapping
    @ApiOperation(value = "添加或修改")
    @PreAuthorize(name = "添加或修改部门",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "批量删除部门",sn = "department:patchDel")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            //System.out.println(ids);
            departmentService.patchDelete(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation("查询部门树")
    @PreAuthorize(name = "查询部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
